package com.digitalchina.lxnsy.view.ChannelView;

import android.content.Intent;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.ItemTouchHelper;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.base.BaseActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 频道 增删改查 排序
 * Created by YoKeyword on 15/12/29.
 */
public class ChannelActivity extends BaseActivity {

    private RecyclerView mRecy;
    private List<ChannelEntity> otherItems = new ArrayList<>();
    private List<ChannelEntity> myItems = new ArrayList<>();
    private Map<String, List<ChannelEntity>> map = new HashMap<>();
    int index = 0;
    private ChannelAdapter adapter;

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_channel);
        setTitle("农产品管理");
    }

    @Override
    public void initMember() {
        mRecy = (RecyclerView) findViewById(R.id.recy);
        map = (Map<String, List<ChannelEntity>>) getIntent().getSerializableExtra("map");
        myItems = map.get("myList");
        otherItems = map.get("otherList");


        GridLayoutManager manager = new GridLayoutManager(this, 4);
        mRecy.setLayoutManager(manager);

        ItemDragHelperCallback callback = new ItemDragHelperCallback();
        final ItemTouchHelper helper = new ItemTouchHelper(callback);
        helper.attachToRecyclerView(mRecy);

        adapter = new ChannelAdapter(this, helper, myItems, otherItems);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int viewType = adapter.getItemViewType(position);
                return viewType == ChannelAdapter.TYPE_MY || viewType == ChannelAdapter.TYPE_OTHER ? 1 : 4;
            }
        });
        mRecy.setAdapter(adapter);

        adapter.setOnMyChannelItemClickListener(new ChannelAdapter.OnMyChannelItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                Toast.makeText(ChannelActivity.this, myItems.get(position).getName(), Toast.LENGTH_SHORT).show();
            }
        });
    }


    @Override
    public void addListener() {
        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("myChannelList", (Serializable) adapter.getMyChannelList());
                setResult(999, intent);
                finish();
            }
        });
    }

    @Override
    public void setHandler() {

    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = new Intent();
            intent.putExtra("myChannelList", (Serializable) adapter.getMyChannelList());
            setResult(999, intent);
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}
